package org.jash.shop2201.user

import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.viewModelScope
import kotlinx.coroutines.launch
import org.jash.shop2201.entity.User
import org.jash.shop2201.mvvm.BaseViewModel
import org.jash.shop2201.net.service

class LoginViewModel:BaseViewModel() {
    val userLiveData by lazy { MutableLiveData<User>() }
    fun login(map:Map<String, String>) {
        viewModelScope.launch {
            try {
                val res = service.login(map)
                if (res.code == 200) {
                    userLiveData.postValue(res.data)
                } else {
                    errorLiveData.postValue(RuntimeException(res.message))
                }
            } catch (e:Exception) {
                errorLiveData.postValue(e)
            }
        }
    }
}